package com.lqs.sofp.servlet;

import com.alibaba.fastjson.JSON;
import com.lqs.sofp.entity.Text;
import com.lqs.sofp.service.TextService;
import com.lqs.sofp.service.impl.TextServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
 * @author cj&lkh
 */
@WebServlet(name = "SelectAllTextServlet", value = "/Text/sats")
public class SelectAllTextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TextService textService = new TextServiceImpl();

        //调用service查询
        List<Text> textList = textService.getText();
        //将数据逆序化以实现：使得最新发布的信息会插入在表头。
        Collections.reverse(textList);
        //将数据JSON化
        String jsonString = JSON.toJSONString(textList);

        //响应数据
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
